package com.zzyl.nursing.controller.member;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.zzyl.common.core.domain.R;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.dto.MemberReservationPageDto;
import com.zzyl.nursing.vo.ReservationCountVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zzyl.common.annotation.Log;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.enums.BusinessType;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.service.IReservationService;
import com.zzyl.common.utils.poi.ExcelUtil;
import com.zzyl.common.core.page.TableDataInfo;

/**
 * 预约信息Controller
 *
 * @author ruoyi
 * @date 2024-09-20
 */
@Slf4j
@RestController
@RequestMapping("/member/reservation")
@Api(tags = "预约信息的接口")
public class MemberReservationController extends BaseController {
    @Autowired
    private IReservationService reservationService;

    /**
     * 分页查询预约信息列表
     */
    @ApiOperation("查询预约信息列表")
    @GetMapping("/page")
    public R seleReservationList(MemberReservationPageDto reservation) {
        reservation.setCreate_by(UserThreadLocal.getUserId());
        List<Reservation> list = reservationService.seleReservationList(reservation);
        return R.ok(getDataTable(list));
    }

    /**
     * 获取预约信息详细信息
     */
    @ApiOperation("获取预约信息详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@ApiParam(value = "预约信息ID", required = true)
                              @PathVariable("id") Long id) {
        return success(reservationService.selectReservationById(id));
    }

    /**
     * 新增预约信息
     */
    @ApiOperation("新增预约信息")
    @Log(title = "预约信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@ApiParam(value = "预约信息实体")
                          @RequestBody Reservation reservation) {
        return toAjax(reservationService.insertReservation(reservation));
    }

    /**
     * 修改预约信息
     */
    @ApiOperation("修改预约信息")
    @Log(title = "预约信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@ApiParam(value = "预约信息实体")
                           @RequestBody Reservation reservation) {
        return toAjax(reservationService.updateReservation(reservation));
    }

    /**
     * 删除预约信息
     */
    @ApiOperation("删除预约信息")
    @Log(title = "预约信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(reservationService.deleteReservationByIds(ids));
    }

    /**
     * 查询取消预约数量
     *
     * @return
     */
    @ApiOperation("查询取消预约数量")
    @GetMapping("/cancelled-count")
    public R<Integer> getCancelledReservationCount() {
        //获取当前线程的用户id
        Long userId = UserThreadLocal.getUserId();
        return R.ok(reservationService.getCancelledReservationCount(userId));
    }

    /**
     * 查询每个时间段剩余预约次数
     *
     * @return
     */
    @ApiOperation("查询每个时间段剩余预约次数")
    @GetMapping("/countByTime")

    public R<List<ReservationCountVo>> getRemainCount(Long time) {
        Instant instant = Instant.ofEpochMilli(time);
        LocalDateTime now = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
        return R.ok(reservationService.getRemainCount(now));
    }


    /**
     * 取消预约
     *
     * @param id
     * @return
     */
    @ApiOperation("取消预约")
    @PutMapping("/{id}/cancel")
    public R cancel(@PathVariable Long id) {
        Reservation reservation = reservationService.selectReservationById(id);
        reservation.setStatus(2);
        return R.ok(reservationService.updateReservation(reservation));
    }
}
